// pages/zero-yuan-clap-details/details.js
import {
  ajax,
  src,
  util
} from "../../utils/util.js";
Page({

  /**
   * 页面的初始数据
   */
  data: {
    vertical: false,
    autoplay: false,
    interval: 3000,
    duration: 500,
    circular: true,
    id:'',
    bidshow: false,
    succeedshow: false,
    details:'',
    record:'',
    time:'',
    shi:'',
    fen:'',
    miao:'',
    comment:'',
    publishshow:false,
    focus:false,
    value:'',
    lingxianjia: 0,
    chujia:'',
    timer:'',
    showyajin:false,
     yjid: ""
  },



  //退出出家弹窗
  tcchujiatc: function () {
    this.setData({
      bidshow: false
    })
  },
  
  // 获取商品详情
  getdetails: function () {
    ajax('/api/index/good_show', {
      data: {
        uqid: wx.getStorageSync("uqid"),
        id:this.data.id
      },
      header: {
        'Accept': 'application/x-www-form-urlencoded', // 默认值
      },
      method: "get",
    }, res => {
      var arr = res.data
      var li1 = '.jpg', li2 = '.jpeg', li3 = '.png', li4 = '.gif';
      arr.plan_arrs = []
      if (arr.plan_arr) {
        for (var a in arr.plan_arr) {
          if (arr.plan_arr[a].includes(li1) || arr.plan_arr[a].includes(li2) || arr.plan_arr[a].includes(li3) || arr.plan_arr[a].includes(li4)) {
            arr.plan_arrs[a] = { 'url': arr.plan_arr[a], 'style': '0' }
          } else {
            arr.plan_arrs[a] = { 'url': arr.plan_arr[a], 'style': '1' }
          }
        }
      }
      this.setData({
        details: res.data,
        time: res.data.diff_time
      })
      this.altertime()
    })
  },
  // 出价记录
  getrecord: function () {
    ajax('/api/index/bid_log_list', {
      data: {
        uqid: wx.getStorageSync("uqid"),
        id: this.data.id
      },
      header: {
        'Accept': 'application/x-www-form-urlencoded', // 默认值
      },
      method: "get",
    }, res => {

      this.setData({
        record:res.data
      })
    })
  },

  // 定时任务

  renwu:function(){
    var that=this
    that.data.renwu=setInterval(function(){
      clearInterval(that.data.timer);

      that.getdetails()
      that.getrecord()
    },5000)
  },


  // 评论记录
  getcomment:function(){
    ajax('/api/index/forum_show_comment', {
      data: {
        type:3,
        id: this.data.id,
        page:1,
        limit:1
      },
      header: {
        'Accept': 'application/x-www-form-urlencoded', // 默认值
      },
      method: "get",
    }, res => {

      this.setData({
        comment: res.data
      })
    })
  },
  // 查看更多评论
  seemore:function(e){
    var id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: '/pages/comment-list/list?id=' + id,
    })
  },
  // 收藏 
  shoucang:function(){
    ajax('/api/user/favorite_main', {
      data: {
        uqid: wx.getStorageSync("uqid"),
        goods_id: this.data.id
      },
      header: {
        'Accept': 'application/x-www-form-urlencoded', // 默认值
      },
      method: "put",
    }, res => {
      this.getdetails()
    })
  },
  // 提醒我
  submitInfo: function (e) {
    ajax('/api/user/remind', {
      data: {
        uqid: wx.getStorageSync("uqid"),
        id: this.data.id,
        form_id: e.detail.formId
      },
      header: {
        'Accept': 'application/x-www-form-urlencoded', // 默认值
      },
      method: "POST",
    }, res => {
      if (res.error == 0) {
        wx.showToast({
          title: '提醒成功',
          icon: 'success',
          duration: 2000
        })
      } else {
        wx.showToast({
          title: res.msg,
          duration: 2000
        })
      }
    })
  },
  // 分隔  时分秒  剩余时间  
  altertime: function () {
    var time = this.data.time;
    var arr = time.split(":");
    this.setData({
      shi: arr[0],
      fen: arr[1],
      miao: arr[2]
    })
    this.settime()
  },
  // 定时器  一秒走一次
  settime: function () {
    var that = this
    that.data.timer = setInterval(function () {
      var miao = that.data.miao -= 1
      var fen = that.data.fen
      var shi = that.data.shi
      if (miao <= 0) {
        fen -= 1
        miao = 60
      }
      if (fen == -1) {
        shi -= 1
        fen = 60
      }

      that.setData({
        miao: miao,
        fen: fen,
        shi: shi
      })

      if (shi == -1) {
        clearInterval(that.data.timer)
        console.log("计时结束")
        setTimeout(function(){
          that.getdetails()
        },2000 )
      }

    }, 1000)
  },
  // 显示出价矿
  bid: function (e) {
    var price = e.currentTarget.dataset.chujia
    var id = e.currentTarget.dataset.id
    ajax('/api/index/is_deposit', {
      data: {
        uqid: wx.getStorageSync("uqid"),
        goods_id: id
      },
      header: {
        'Accept': 'application/x-www-form-urlencoded', // 默认值
      },
      method: "get",
    }, res => {
      if (res.data.status == 1) {
        this.setData({
          bidshow: true,
          chujia: price,
          id: id
        })
        // wx.showToast({
        //   title: '支付成功',
        //   icon: 'succes',
        //   duration: 1000,
        //   mask: true
        // })
      } else {
        ajax('/api/mall/deposit_pay', {
          data: {
            uqid: wx.getStorageSync("uqid"),
            goods_id: id
          },
          header: {
            'Accept': 'application/x-www-form-urlencoded', // 默认值
          },
          method: "POST",
        }, res => {

         this.setData({
           showyajin:true,
           yjid:id
         })

          this.chajine()
        })
      }
    })

  },



  //退出押金弹窗
  tctcyj: function () {
    this.setData({
      showyajin: false
    })
  },

  //查询押金金额
  chajine: function () {
    ajax('/api/user/check_auction_deposit', {
      data: {
        uqid: wx.getStorageSync("uqid"),
        goods_id: this.data.yjid
      },
      header: {
        'Accept': 'application/x-www-form-urlencoded', // 默认值
      },
      method: "get",
    }, res => {
      this.setData({ yjje: res.data.margin })
    })
  },

  // // 去交押金
  // topayyajin: function (e) {
  //   var id = e.currentTarget.dataset.id
  //   console.log(e)
  //   this.setData({
  //     showyajin: true,
  //     yjid: id
  //   })

  //   this.chajine()
  // },

  queding: function (id) {
    this.surepay()
  },

  // 交押金
  surepay: function () {
    this.setData({
      showyajin: false
    })
    ajax('/api/mall/deposit_pay', {
      data: {
        uqid: wx.getStorageSync("uqid"),
        goods_id: this.data.yjid
      },
      header: {
        'Accept': 'application/x-www-form-urlencoded', // 默认值
      },
      method: "post",
    }, res => {
      if (res.error == 0) {
        wx.requestPayment({
          timeStamp: res.data.sin_data.timeStamp,
          nonceStr: res.data.sin_data.nonceStr,
          package: res.data.sin_data.package,
          signType: res.data.sin_data.signType,
          paySign: res.data.sin_data.paySign,

          success(res) {
            console.log(res)
            // this.getzeropatlist()
          },
          fail(res) {
            console.log(res)
          }
        })
      } else if (res.error == 200) {
        wx.showToast({
          title: '押金支付成功',
          icon: 'succes',
          duration: 1000,
          mask: true
        })
      } else {
        wx.showToast({
          title: res.msg,
          icon: 'succes',
          duration: 1000,
          mask: true
        })
      }
    })
  },





  // 获取出价
  valueInput: function (e) {
    console.log(e)
    var reg = /^[0-9]+.?[0-9]*$/;
    if (reg.test(e.detail.value)) {
      var qian = parseFloat((e.detail.value - this.data.details.spike_price).toFixed(10))
      this.setData({
        verify: e.detail.value,
        lingxianjia: qian
      });
    }

  },
  // 确认出价
  surebid: function () {
    this.setData({
      bidshow: false,
    })
    ajax('/api/mall/bid_add', {
      data: {
        uqid: wx.getStorageSync("uqid"),
        goods_id: this.data.id,
        price: this.data.verify
      },
      header: {
        'Accept': 'application/x-www-form-urlencoded', // 默认值
      },
      method: "post",
    }, res => {
      if (res.error == 0) {
        this.setData({
          bidshow:false,
          // succeedshow: true,
          verify: 0,
          lingxianjia: 0
        })

        wx.showToast({
          title: '出价成功',
          duration: 1000,
          mask: true
        })
      } else {
        wx.showToast({
          title: res.msg,
          duration: 1000,
          mask: true
        })
        this.setData({
          verify: 0,
          lingxianjia: 0
        })
      }

    })
  },
  // 回到首页出价成功矿
  quxiao: function () {
    this.setData({
      succeedshow: false
    })
    wx.reLaunch({
      url: '/pages/index/index',
    })
  },
  // 查看竞拍出价成功框
  queren: function () {
    this.setData({
      succeedshow: false
    })
    wx.navigateTo({
      url: '/pages/my-auction/auction',
    })
  },

  // 一级评论
  pinglun:function(){
    this.setData({
      publishshow: true
    });
  },
  // 获取输入框内容
  inputvalue: function (e) {
    this.setData({
      value: e.detail.value,
      focus: true,
    });
  },
  // 发表1级
  fabiaos: function () {
    ajax('/api/user/comment_first_add', {
      data: {
        uqid: wx.getStorageSync("uqid"),
        type: 3,
        zid: this.data.id,
        content: this.data.value
      },
      header: {
        'Accept': 'application/x-www-form-urlencoded', // 默认值
      },
      method: "post",
    }, res => {
      this.getcomment()
      this.setData({
        publishshow: false,
        focus: false,
        value: '',
      })
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      id:options.id
    })
    this.getdetails()
    this.getrecord()
    this.getcomment()

    this.renwu()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    let e = this;
    clearInterval(e.data.timer);
    clearInterval(e.data.renwu)
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    let e = this;
    clearInterval(e.data.timer);
    clearInterval(e.data.renwu)
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})